package tongwentongshu.com.app.activity;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import tongwentongshu.com.app.R;
import tongwentongshu.com.app.adapter.MessageListAdapter;
import tongwentongshu.com.app.adapter.MyCollectionAdapter;
import tongwentongshu.com.app.bean.MessageBean;
import tongwentongshu.com.app.bean.MyCollectionBean;
import tongwentongshu.com.app.contract.MyCollectionContract;
import tongwentongshu.com.app.contract.MyMessageListContract;
import tongwentongshu.com.app.presenter.MyCollectionPresenter;
import tongwentongshu.com.app.presenter.MyMessageListPresenter;

/**
 * Created by XiongBin on 2017/7/18.
 */

public class MyMessageListActivity extends BaseActivity implements MyMessageListContract.View, BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.rv_list)
    RecyclerView mRecyclerView;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout mSwipeRefreshLayout;
    private MessageListAdapter messageListAdapter;
    protected boolean onRefreshJudge = false;
    private MyMessageListPresenter myMessageListPresenter;

    @Override
    protected int getLayoutResourceId() {
        return R.layout.activity_message_list;
    }

    @Override
    protected int getTitleId() {
        if (getIntent().getExtras().getString("type").equals("1"))
            return R.string.msg_1;
        else
            return R.string.msg_2;
    }

    @Override
    protected void initView() {
        onRefreshJudge = true;
        mSwipeRefreshLayout.setOnRefreshListener(this);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        List arr = new ArrayList<MyCollectionBean.DataBean>();
        messageListAdapter = new MessageListAdapter(mContext, arr);
        messageListAdapter.setOnLoadMoreListener(this, mRecyclerView);
        messageListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);

        mRecyclerView.setAdapter(messageListAdapter);
        mRecyclerView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(final BaseQuickAdapter adapter, final View view, final int position) {
                if (msg.getData().get(position).getMessage_style().equals("1")) {
                    Bundle b = new Bundle();
                    b.putBoolean("pager_Judge", false);
                    b.putString("pagerType", "1");
                    b.putString("bookId", msg.getData().get(position).getBook_id());
                    startActivity(ReadingDetailsActivity.class, b);
                } else if (msg.getData().get(position).getMessage_style().equals("2")) {
                    startActivity(ShudouActivity.class);
                } else if (msg.getData().get(position).getMessage_style().equals("3")) {
                    startActivity(DepositDetailsActivity.class);
                } else if (msg.getData().get(position).getMessage_style().equals("4")) {
                    Bundle b = new Bundle();
                    if (msg.getData().get(position).getIs_auth().equals("1")) {
                        b.putBoolean("pager_Judge", true);
                    } else {
                        b.putBoolean("pager_Judge", false);
                    }
                    b.putString("pagerType", "1");
                    b.putString("bookId", msg.getData().get(position).getBook_id());
                    startActivity(ReadingDetailsActivity.class, b);

                } else {
                    Bundle b = new Bundle();
                    b.putString("url", msg.getData().get(position).getUrl());
                    startActivity(WebViewActivity.class, b);

                }
                myMessageListPresenter.chengeMessage(msg.getData().get(position).getMessid());

            }
        });
    }

    @Override
    protected void initData() {
        myMessageListPresenter = new MyMessageListPresenter(this);

    }

    @Override
    protected void onResume() {
        super.onResume();
        onRefreshJudge = true;
        myMessageListPresenter.getMyMessageList(getIntent().getExtras().getString("type"));
    }

    MessageBean msg;

    @Override
    public void onSuccess(MessageBean msg) {
        View empty = getLayoutInflater().inflate(R.layout.empty_no_data, (ViewGroup) mRecyclerView.getParent(), false);
        messageListAdapter.setEmptyView(empty);
        this.msg = msg;
        if (msg.getData() != null && msg.getData().size() > 0) {
            if (onRefreshJudge) {
//                Log.e(TAG, "onRefreshJudge: " + onRefreshJudge);
                messageListAdapter.setNewData(msg.getData());
                mSwipeRefreshLayout.setRefreshing(false);
                messageListAdapter.setEnableLoadMore(true);
                onRefreshJudge = false;
            }
        } else {
            mSwipeRefreshLayout.setRefreshing(false);
            messageListAdapter.setEnableLoadMore(true);
            onRefreshJudge = false;
        }
    }

    @Override
    public void onShowError(String msg) {

    }

    @Override
    public void onRefresh() {
        onRefreshJudge = true;
        myMessageListPresenter.getMyMessageList(getIntent().getExtras().getString("type"));
    }

    @Override
    public void onLoadMoreRequested() {
        messageListAdapter.loadMoreEnd(true);
    }
}
